<?php
/**
 * Created by PhpStorm.
 * Author: 赵凯
 * Date: 2019/12/10
 * Time: 16:08
 */

namespace api\install\controller;


use api\common\model\RlApplyDelayModel;
use api\common\model\RlBmNodeRelModel;
use api\common\model\RlCompanyModel;
use api\common\model\RlNodeModel;
use api\common\model\RlTypeModel;
use api\common\model\RlEtAzdModel;
use api\common\model\RlEtAzsModel;
use api\common\model\RlEtCompanyModel;
use api\common\model\RlEtCustomerModel;
use api\common\model\RlHiseServiceProjectModel;
use api\common\model\RlZsmsUserQueryModel;
use api\common\service\CurlService;
use Knp\Snappy\Pdf;
use Mpdf\Mpdf;
use think\Db;
use api\common\service\FunctionService;
use think\exception\DbException;
use api\common\service\MsgService;
use api\common\yueke\YuekeController;
class AnzhuangshangController extends BaseController
{
    protected $status = [
        '未进场' => 0,
        '已进场' => 1,
        '隐蔽施工中' => 2,
        '隐蔽施工已结束' => 3,
        '调试' => 4,
        '运行' => 5,
        '作废' => 'X',

    ];
    protected $install_status_new = [
        0 => '未进场',
        1 => '已进场',
        2 => '隐蔽施工中',
        3 => '隐蔽施工已结束',
        4 => '调试',
        5 => '运行',
        'X' => '作废',
    ];
    protected $dispatch = [

        '待安装商派工' => 1,//安装商
        '待安装队接受' => 2,//安装商
        '安装队已接受' => 3,//安装队
        '安装商已退回' => 4,
        '安装队已退回' => 5,


    ];

    //已完成
    public function alreadyInstalled()
    {
        $install_status = $this->status['运行'];
        // $jxs = $this->request->param('jxs');
        // if(!$jxs) $this->error('参数错误');
        $where['gczt'] = $install_status;
        $where['zzdpdzt'] = $this->dispatch['安装队已接受'];
        // $where['jxs'] = $jxs;
//        $where['azd'] = $this->user_info['engineer_code'];
        $where['azs'] = $this->user_info['server_code'];
        $selectdata = $this->request->param();
        $funt = new FunctionService;
        $selectazd = $funt->selectazd($selectdata);
        $where = array_merge($where, $selectazd);
        $pege = input('param.pege');
        $limit = input('param.limit');
        $start = ($pege - 1) * $limit;
        $field = 'azd.engineer_name,hise.yh_name,hise.azd_id,hise.address,hise.provice,hise.city,hise.qx,hise.yyaz_sj,hise.fgsfwfzr,hise.gczt,hise.jxsxsy,hise.kqshzt,hise.sfkaz_bs,hise.sfkq_bs,hise.hise_service_project_id,zzdpdzt';
        $list = RlHiseServiceProjectModel::getHiseList($where,$field,$start,$limit);
        // 遍历格式化数据等信息
        foreach ($list as $key => $val) {
            $list[$key]['problem_num'] = Db::name('rl_inspect')->where('azd_id', $val['azd_id'])->where('is_problem', 1)->where('inspect_status', 2)->count();
            $list[$key]['gczt_name'] = $this->install_status_new[$val['gczt']];
        }
        $data['user_info'] = $this->user_info;
        $data['data'] = $list;
        $this->success('请求成功', '', $data);
    }

    //正在进行
    public function waitInstalled()
    { 
        // $jxs = $this->request->param('jxs','');
        // if(!$jxs) $this->error('参数错误');
        $where['gczt'] = ['in', $this->status['未进场'] . ',' . $this->status['已进场'] . ',' . $this->status['隐蔽施工中'] . ',' . $this->status['隐蔽施工已结束'] . ',' . $this->status['调试']];
        $where['zzdpdzt'] = $this->dispatch['安装队已接受'];
//        $where['azd'] = $this->user_info['engineer_code'];
        $where['azs'] = $this->user_info['server_code'];
        $selectdata = $this->request->param();
        $funt = new FunctionService;
        $selectazd = $funt->selectazd($selectdata);
        $where = array_merge($where, $selectazd);
        $pege = input('param.pege');
       if(!$pege){
        $pege=1;
       }
        $limit = input('param.limit');
        $start = ($pege - 1) * $limit;
       if(!$start){
      $start=0;
      }
    //  dump($start); dump($pege);die();
       
        $field = 'azd.engineer_name,hise.yh_name,hise.azd_id,hise.address,hise.provice,hise.city,hise.qx,hise.yyaz_sj,hise.fgsfwfzr,hise.gczt,hise.jxsxsy,hise.kqshzt,hise.sfkaz_bs,hise.sfkq_bs,hise.hise_service_project_id,zzdpdzt';
        $list = RlHiseServiceProjectModel::getHiseList($where,$field,$start,$limit);
        //dump($list);die;
        //$list = RlHiseServiceProjectModel::getHiseList($where,$field);
        // 遍历格式化数据等信息
       // foreach ($list as $key => $val) {
           // $list[$key]['problem_num'] = Db::name('rl_inspect')->where('azd_id', $val['azd_id'])->where('is_problem', 1)->where('inspect_status', 2)->count();
         //   $list[$key]['gczt_name'] = $this->install_status_new[$val['gczt']];
       // }
        $data['user_info'] = $this->user_info;
        $data['data'] = $list;

        $this->success('请求成功', '', $data);
    }

    //待接收
    public function waitReceived()
    {
        $dispatch_status = $this->dispatch['待安装队接受'];
        $where['zzdpdzt'] = $dispatch_status;
        $where['gczt'] = ['<>', 'X'];
//        $where['azd'] = $this->user_info['engineer_code'];
        $where['azs'] = $this->user_info['server_code'];
        $selectdata = $this->request->param();
        $funt = new FunctionService;
        $selectazd = $funt->selectazd($selectdata);
        $where = array_merge($where, $selectazd);
        $pege = input('param.pege');
        $limit = input('param.limit');
        $start = ($pege - 1) * $limit;
        $field = 'azd.engineer_name,hise.yh_name,hise.azd_id,hise.address,hise.provice,hise.city,hise.qx,hise.yyaz_sj,hise.fgsfwfzr,hise.gczt,hise.jxsxsy,hise.kqshzt,hise.sfkaz_bs,hise.sfkq_bs,hise.hise_service_project_id,zzdpdzt';
        $list = RlHiseServiceProjectModel::getHiseAzsList($where,$field,$start,$limit);
        // 遍历格式化数据等信息
        foreach ($list as $key => $val) {
            $list[$key]['problem_num'] = Db::name('rl_inspect')->where('azd_id', $val['azd_id'])->where('is_problem', 1)->where('inspect_status', 2)->count();
            $list[$key]['gczt_name'] = $this->install_status_new[$val['gczt']];
        }
        $data['user_info'] = $this->user_info;
        $data['data'] = $list;
        $this->success('请求成功', '', $data);
    }

    //接受/拒绝派工单
    public function acceptInstall()
    {
        $service_project_id = $this->request->param('service_project_id/a', []);
        $dispatch_status = $this->request->param('dispatch_status', 3);
        $azd = $this->request->param('azd');
        $zzdpdzt = RlHiseServiceProjectModel::Where('hise_service_project_id', 'in', $service_project_id)->select()->toArray();
        foreach ($zzdpdzt as $key => $value) {
            if ($value['zzdpdzt'] != 2) {
                $this->success('不能进行此操作');
            }
            $bbdh=$value['bbdh'];
        }
        $status = 0;
        if ($dispatch_status == 5) {
            $status = $this->status['未进场'];
            $data['azs'] = '';
        }else if(!$azd)
        {
            $this->success('请选择安装队');
        }
        if (empty($service_project_id)) $this->error('ID不能为空');

        $data['azd'] = $azd?$azd:'';
        $data['gczt'] = $status;
        $data['zzdpdzt'] = $dispatch_status;
        $data['update_time'] = time();
        $res = RlHiseServiceProjectModel::Where('hise_service_project_id', 'in', $service_project_id)->update($data);

        $msg = '拒绝';
        if ($dispatch_status == 3) $msg = '接受';
        if ($res) {
            if (config('installsend2') && $dispatch_status != 3) {
                // 引用发送验证码类
                $msg_send = new MsgService();
                //获取安装队手机号信息
                foreach ($zzdpdzt as $v) {

                    $azd_id = $v['azd_id'];
                    $phone = $v['xsy_sj'];
                    // 发送手机验证码
                    $msgres = $msg_send->installsend2($azd_id, $phone);
                    if ($msgres) {
                        trace($phone . '||' . date('Y-m-d h:i:s', time()) . 'installsend2短信发送成功', 'info');
                    } else {
                        trace($phone . '||' . date('Y-m-d h:i:s', time()) . 'installsend2短信发送失败', 'info');
                    }
                }
            }
            $yueke = new YuekeController();
            $data = $yueke->postAzsParam($bbdh);
            $this->success($msg . '安装单成功');
        }
        $this->error($msg . '安装单失败');

    }

    //工程信息
    public function installInfo()
    {
        $service_project_id = $this->request->param('service_project_id', '');
        if (!$service_project_id) $this->error('参数错误');
        //多表引入查询方法
        $info = RlHiseServiceProjectModel::Where('hise_service_project_id', $service_project_id)->field('yh_name,xb,sj_one,provice,city,qx,address,qyfgs,jxs
        ,azs,azd,fgsfwfzr,cplb,yyjc_sj,jkh,zjbxq,zybjbxq,jxsxsy,bz,pic_one,pic_two,yyaz_sj,xsy_sj')->find();
        if (!$info) $this->error('该用户不存在');
        if (empty($info['qx'])) {
            $city = Db::name('rl_hise_dq_table')->where('dq_code', $info['city'])->field('dq_name')->find()['dq_name'];
            $provice = Db::name('rl_hise_area')->where('sf_code', $info['provice'])->field('sf_name')->find()['sf_name'];
            $info['address_detail'] = $provice . $city . $info['address'];
            $info['address_cp'] = $info['address'];
            $info['address'] = $provice . $city;
        } else {
            $xq = Db::name('rl_hise_qx_table')->where('qx_code', $info['qx'])->field('qx_name')->find()['qx_name'];
            $city = Db::name('rl_hise_dq_table')->where('dq_code', $info['city'])->field('dq_name')->find()['dq_name'];
            $provice = Db::name('rl_hise_area')->where('sf_code', $info['provice'])->field('sf_name')->find()['sf_name'];
            $info['address_detail'] = $provice . $city . $xq . $info['address'];
            $info['address_cp'] = $info['address'];
            $info['address'] = $provice . $city . $xq;
        }
        $info['jxs_name'] = RlEtCustomerModel::where('jxsbm', $info['jxs'])->field('jxs_mc')->find()['jxs_mc'];
        $info['azs_name'] = RlEtAzsModel::where('server_code', $info['azs'])->field('server_name')->find()['server_name'];
        $info['azd_name'] = RlEtAzdModel::where('engineer_code', $info['azd'])->field('engineer_name')->find()['engineer_name'];
        $info['azd_phone'] = RlEtAzdModel::where('engineer_code', $info['azd'])->field('phone')->find()['phone'];
        $info['fgs_name'] = Db::name('rl_company')->where('orgdept', $info['qyfgs'])->field('orgname')->find()['orgname'];

        $cplb_arr = explode('/', $info['cplb']);
        $cplb_str = implode(',', $cplb_arr);
        $product_name_arr = Db::name('rl_et_product_xcx')->where('product_id', 'in', $cplb_str)->column('product_name');
        $info['cplb'] = implode('/', $product_name_arr);

        if ($info['yyjc_sj']) {
            $info['yyjc_sj'] = date('Y-m-d', $info['yyjc_sj']);
        }
        if ($info['yyaz_sj']) {
            $info['yyaz_sj'] = date('Y-m-d', $info['yyaz_sj']);
        }
        $data['info'] = $info;
        $this->success('请求成功', '', $data);


    }

    //返回安装商下的安装队人员
    public function azdman()
    {
        $azd_data = RlEtAzdModel::where('server_code',$this->user_info['server_code'])->where('post',2)->where('active_flag','<>',0)->select()->toArray();
        $this->success('请求成功', '', $azd_data);
    }
}
